<%@page import="com.liferay.portal.webserver.WebServerServletTokenUtil"%>
<%@ include file="/html/init.jsp" %>

<%@page import="com.util.CartUtil"%>
<%@ page import="com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil" %>

<%
	long itemId = ParamUtil.getLong(request, "itemId");

	ShoppingItem item = ShoppingItemLocalServiceUtil.fetchShoppingItem(itemId);
	
	String backURL = ParamUtil.getString(request, "backURL");
	
	PortletURL addToCartURL = renderResponse.createActionURL();
	addToCartURL.setParameter(ActionRequest.ACTION_NAME, "addToCart");
	
	int amount = (int) item.getPrice();
	
	boolean variableAmount = (amount == 0);
	pageContext.setAttribute("variableAmount", variableAmount);
	pageContext.setAttribute("frequencyMap", CartUtil.getFrequencyMap(itemId));
	ShoppingItem shoppingitem= ShoppingItemLocalServiceUtil.getItem(itemId);
	shoppingitem.getSmallImageId();
	shoppingitem.getDescription();
%>

<liferay-ui:header
	backLabel="&laquo; Back to List"
	title="<%= item.getName()%>" 
	backURL="<%= backURL %>"
/>
<img alt="test" src="<%=  themeDisplay.getPathImage() + "/shopping/item?img_id=" + shoppingitem.getSmallImageId()+ "&t=" + WebServerServletTokenUtil.getToken(	shoppingitem.getSmallImageId()) %>" vspace="0" /><br>
<%=	shoppingitem.getDescription() %>
<aui:form action="<%= addToCartURL %>" method="POST" name="fm">
	<aui:input name="itemId" value="<%= itemId %>" type="hidden"/>

	<aui:column>
		<aui:select name="qty" label="Quantity" onChange="calculateAmount(this.value);">
			<c:forEach items="<%= IConstants.QTY_LIST %>" var="entry">
				<c:if test="${!(variableAmount && entry > 1)}">
					<aui:option value="${entry}">${entry}</aui:option>
				</c:if>
			</c:forEach>
		</aui:select>
	</aui:column>

	<aui:column>
		<aui:select name="frequency" label="Frequency" >
			<c:forEach items="${frequencyMap}" var="frequencyMap">
				<aui:option value="${frequencyMap.key}">${frequencyMap.value}</aui:option>
			</c:forEach>
		</aui:select>
	</aui:column>

	<aui:column>
		<c:choose>
			<c:when test="<%= variableAmount %>">
				<aui:input name="amount" label="Amount">
					<aui:validator name="required"/>
					<aui:validator name="number"/>
					<aui:validator name="range">[10,99999]</aui:validator>
				</aui:input>
			</c:when>
			<c:otherwise>
				<aui:input name="amount" label="Amount" value="<%= amount %>" readonly="readonly"/>
			</c:otherwise>
		</c:choose>
	</aui:column>

	<aui:button-row>
		<aui:button type="submit" value="Add To Cart"/>
	</aui:button-row>
</aui:form>
<aui:script>
	function calculateAmount(qty){
		var total = qty * <%= amount %>;
		document.getElementById("<portlet:namespace/>amount").value = total;
	}
	
	<c:if test="<%= variableAmount %>">
		Liferay.Util.focusFormField(document.<portlet:namespace />fm.<portlet:namespace />amount);
	</c:if>
</aui:script>